package com.loe.ktui

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import com.google.android.flexbox.FlexWrap
import com.loe.ktui.ui.*
import com.loe.ktui.ui_custom.*
import com.loe.ktui.util.KtColor
import com.loe.ktui.util.KtFragItem
import com.loe.ktui.util.ui_Shape

class MainActivity : AppCompatActivity()
{
    private val textDemo: KtTextView by lazy { findView("textDemo") }

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)

        contentUI = ui_LinearLayout()
        {
            bg = KtColor.bg

            ui_TitleView()
            {
                title = "测试标题"
                bg = KtColor.white
                statusFull = false
            }

            ui_ScrollView()
            {
                height= MATCH_PARENT
                fillViewport = true
                ui_LinearLayout()
                {
                    width = MATCH_PARENT
                    padding = 8
                    bg = Color.YELLOW
                    divider = ui_Shape { size = 10 }

                    ui_BannerView()
                    {
                        corner = 8
                        height = 180

                        itemUI = ui_FrameLayout()
                        {
                            ui_Image()
                            {
                                id = "image"
                                width = MATCH_PARENT
                                height = MATCH_PARENT
                                scaleType = ImageView.ScaleType.CENTER_CROP
                            }
                        }

                        itemConvert()
                        { bean, i ->
                            val image = findView<ImageView>("image")
                            image.src = bean.imageUrl
                        }

                        datas = listOf(
                            BannerBean("https://img0.baidu.com/it/u=705102279,1721672537&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500"),
                            BannerBean("https://img2.baidu.com/it/u=113744206,1558321864&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=333"),
                            BannerBean("https://img2.baidu.com/it/u=3036691508,3668357645&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800"),
                        )
                    }

                    ui_Tab()
                    {
                        height = 50
                        viewPagerId = "viewPager"
                        index = 1
                        bg = KtColor.white
                        lineBg = ui_Shape { color = KtColor.pink; corner = 5 }
                        isScroll = true
                        scrollOffset = 0.0
                        onItemSelect()
                        { view, isSelect ->
                            view as KtTextView
                            view.setTextColor(if (isSelect) KtColor.red else KtColor.text)
                        }
                        itemConvert()
                        { view, i, bean ->
                            view as KtTextView
                            bean as String
                            view.text = bean
                        }
                        datas = listOf("简传递单", "谁是说的", "是啊", "算是多", "多少岁", "滴撒的", "稍等", "f是的", "地方的", "是的是的")

                        ui_Text()
                        {
                            text = "京哈"
                            textColor = Color.RED
                            gravity = Gravity.CENTER
                            paddingH = 10
                            weight = 1.0
                        }
                    }

                    ui_ViewPager()
                    {
                        id = "viewPager"
                        height = 200
                        fragmentManager = supportFragmentManager
                        fragItems = listOf(
                            KtFragItem(TestFragment::class).addArg("name", "顺哥哥00111"),
                            KtFragItem(TestFragment::class).addArg("name", "顺哥哥122"),
                            KtFragItem(TestFragment::class).addArg("name", "顺哥哥133"),
                            KtFragItem(TestFragment::class).addArg("name", "顺哥哥144"),
                            KtFragItem(TestFragment::class),
                        )
                    }

                    ui_SlideLayout()
                    {
                        ui_Text()
                        {
                            width = MATCH_PARENT
                            height = 80
                            gravity = Gravity.CENTER
                            text = "坚实的收到货"
                            bg = KtColor.yellow
                        }

                        ui_Text()
                        {
                            width = 80
                            height = MATCH_PARENT
                            gravity = Gravity.CENTER
                            text = "删除"
                            isClick = true
                            textColor = KtColor.white
                            bg = KtColor.red
                        }
                    }

                    ui_FlexLayout()
                    {
                        width = MATCH_PARENT
                        ui_Text()
                        {
                            text = "大大脚"
                            margin = 5
                            padding = 5
                            bg = KtColor.white
                        }
                        ui_Text()
                        {
                            text = "都是的"
                            margin = 5
                            padding = 5
                            bg = KtColor.white
                            width = 0
                            flexGrow = 2.0
                        }
                        ui_Text()
                        {
                            text = "大都市"
                            margin = 5
                            padding = 5
                            bg = KtColor.white
                            width = 0
                            flexGrow = 1.0
                        }
                    }

                    ui_CheckText()
                    {
                        text = "就是得到"
                        textSize = 14
                        bg = KtColor.bgDeep
                        padding = 5
                    }

                    ui_Text("textDemo")
                    {
                        text = "大手大脚"
                        textSize = 24
                        textColor = Color.GREEN
                        hint = "决胜巅峰"
                    }

                    ui_Button()
                    {
                        paddingH = 25
                        text = "dds袄袄凹好多好abc"
                    }

                    ui_Edit()
                    {
                        paddingH = 15
                        paddingV = 10
                        isClear = true
                        bg = KtColor.white
                        text = "地方抠脚大汉拒接电话丰富的森林里的看法垮落法"
                        hint = "请输入名称"
                    }

                    ui_List<String>()
                    {
                        type = UiList.TYPE_GRID
                        spanCount = 3
                        bg = KtColor.white
                        isScroll = false
                        headerUI = ui_LinearLayout()
                        {
                            ui_Text()
                            {
                                padding = 10
                                text = "几号回家待发货富东饭店"
                                isClick = true
                                onClick()
                                {
                                    this as KtTextView
                                    text = "洪水过后大使馆的故事是打广告的的感受到个个都是多个多个山东干很多事搞活动时更好地更合适的规划"
                                }
                            }
                        }
                        itemUI = listItem

                        itemConvert()
                        { holder, bean ->
                            val title = findView<TextView>("textTitle")
                            title.text = bean
                        }

                        onLoadData()
                        {
                            delay(1000)
                            {
                                loadOk(listOf("新加的", "好的", "世界电话辅导费", "手机号的", "大家都放假京东方"), it)
                            }
                        }

                        datas = listOf("223是的是的", "大多都是", "刷卡说的就是", "第三方付", "疯疯癫癫", "大幅度", "方法", "是多少顺丰到付", "大多都是", "刷卡说的就是", "第三方付", "疯疯癫癫", "大幅度", "方法", "是多少顺丰到付")

                        onCreateView()
                        {
//                    setData(arrayListOf("3332稍等", "好的"))
                        }
                    }

                    ui_View()
                    {
                        width = 50
                        height = 50
                        bg = Color.CYAN
                    }

                    ui_Text()
                    {
                        text="就到河坊街东方红"
                    }
                }
            }
        }

        // 命令控制
        textDemo.text = "版本版本不不不不"
    }

    private val listItem = ui_FrameLayout()
    {
        ui_LinearLayout()
        {
            ui_Image()
            {
                src = "https://img2.baidu.com/it/u=794528275,4064511912&fm=253&fmt=auto&app=138&f=JPEG?w=789&h=500"
            }

            ui_Text("textTitle")
            {
                text = "法等多个"
                textColor = Color.MAGENTA
                hint = "是"
                isClick = true
                onClick = {
                    Toast.makeText(baseContext, "几号回家辅导费", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }

    override fun onResume()
    {
        super.onResume()

        Log.d("runtime", supportFragmentManager.fragments.size.toString())
    }
}